﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SimpleControl
{
    [ToolboxData("<{0}:MyLabel runat='server' myX='0' myY='0' myWidth='100' myHeight='12' myBold = 'False' myItalic='False' MyUnderline='False' Align='0' myFontName='Times New Roman' myFontSize='10' myTextColor='#000000' myText='Simple Text' ><{0}:MyLabel>")]
    public class MyText : WebControl
    {
        #region Properties of control
        #region X Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("x of left point"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myX
        {
            get
            {
                //int x = (int)ViewState["myX"];
                if (ViewState["myX"] == null)
                    return 0;
                else
                    return (int)ViewState["myX"];
            }
            set
            {
                ViewState["myX"] = value;
            }
        } 
        #endregion

        #region Y Properties
        [Category("Appearance"),
       DefaultValue("0"),
       Description("y of left point"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myY
        {
            get
            {
                //int? y = (int?)ViewState["myY"];
                if (ViewState["myY"] == null)
                    return 0;
                else
                    return (int)ViewState["myY"];
            }
            set
            {
                ViewState["myY"] = value;
            }
        } 
        #endregion


        #region Width Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("width of text"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myWidth
        {
            get
            {
                //int? width = (int?)ViewState["myWidth"];
                if (ViewState["myWidth"] == null)
                    return 0;
                else
                    return (int)ViewState["myWidth"];
            }
            set
            {
                ViewState["myWidth"] = value;
            }
        } 
        #endregion

        #region Height Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("height of text"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myHeight
        {
            get
            {
                //int? height = (int?)ViewState["myHeight"];
                if (ViewState["myHeight"] == null)
                    return 0;
                else
                    return (int)ViewState["myHeight"];
            }
            set
            {
                ViewState["myHeight"] = value;
            }
        } 
        #endregion

        #region Bold Properties
        [Category("Appearance"),
       DefaultValue("False"),
       Description("bold of text"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual bool myBold
        {
            get
            {
                //bool? bold = (bool?)ViewState["myBold"];
                if (ViewState["myBold"] == null)
                    return false;
                else
                    return (bool)ViewState["myBold"];
            }
            set
            {
                ViewState["myBold"] = value;
            }
        } 
        #endregion

        #region Italic Properties
        [Category("Appearance"),
        DefaultValue("False"),
        Description("Italic of text"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual bool myItalic
        {
            get
            {
                //bool? italic = (bool?)ViewState["myItalic"];
                if (ViewState["myItalic"] == null)
                    return false;
                else
                    return (bool)ViewState["myItalic"];
            }
            set
            {
                ViewState["myItalic"] = value;
            }
        } 
        #endregion

        #region Underline Properties
        [Category("Appearance"),
       DefaultValue("False"),
       Description("Underline of text"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual bool myUnderline
        {
            get
            {
                //bool? underline = (bool?)ViewState["myUnderline"];
                if (ViewState["myUnderline"] == null)
                    return false;
                else
                    return (bool)ViewState["myUnderline"];
            }
            set
            {
                ViewState["myUnderline"] = value;
            }
        } 
        #endregion

        #region Strikeout  Properties
        [Category("Appearance"),
        DefaultValue("False"),
        Description("StrikeThrough of text"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual bool myStrikeThrough
        {
            get
            {
                //bool? strikeThrough = (bool?)ViewState["myStrikeThrough"];
                if (ViewState["myStrikeThrough"] == null)
                    return false;
                else
                    return (bool)ViewState["myStrikeThrough"];
            }
            set
            {
                ViewState["myStrikeThrough"] = value;
            }
        } 
        #endregion

        #region Align  Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("Align of text (0: Left, 1: Center, 2: Right)"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual byte myAlign
        {
            get
            {
                //byte? align = (byte?)ViewState["myAlign"];
                if (ViewState["myAlign"] == null)
                    return 0;
                else
                    return (byte)ViewState["myAlign"];
            }
            set
            {
                ViewState["myAlign"] = value;
            }
        } 
        #endregion

        #region Font Name Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("Font name of text"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual string myFontName
        {
            get
            {
                string font = (string)ViewState["myFontName"];
                if (font == null)
                    return "Times New Roman";
                else
                    return font;
            }
            set
            {
                ViewState["myFontName"] = value;
            }
        } 
        #endregion

        #region Font Size Properties
        [Category("Appearance"),
        DefaultValue("10"),
        Description("Font size of text"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myFontSize
        {
            get
            {
                //int? fontSize = (int?)ViewState["myFontSize"];
                if (ViewState["myFontSize"] == null)
                    return 10;
                else
                    return (int)ViewState["myFontSize"];
            }
            set
            {
                ViewState["myFontSize"] = value;
            }
        } 
        #endregion

        #region Text length Properties
        [Category("Appearance"),
        DefaultValue("10"),
        Description("Text Lenght"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myTextLength
        {
            get
            {
                //int? fontSize = (int?)ViewState["myFontSize"];
                if (ViewState["myTextLength"] == null)
                    return 10;
                return (int)ViewState["myTextLength"];
            }
            set
            {
                ViewState["myTextLength"] = value;
            }
        }
        #endregion

        #region Text Color  Properties
        [Category("Appearance"),
       DefaultValue("0x78000000"),
       Description("Font color of text"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual Int32 myTextColor
        {
            get
            {
                //Int32 textColor = (Int32)ViewState["myTextColor"];
                if (ViewState["myTextColor"] == null)
                    return (Int32)0x00000000;
                else
                    return (Int32)ViewState["myTextColor"];
            }
            set
            {
                ViewState["myTextColor"] = value;
            }
        } 
        #endregion


        #region Text  Properties
        [Category("Appearance"),
       DefaultValue("Simple of Text"),
       Description("Font color of text"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual string myText
        {
            get
            {
                string text = (string)ViewState["myText"];
                if (text == null)
                    return "Simple of Text";
                else
                    return text;
            }
            set
            {
                ViewState["myText"] = value;
            }
        }  
        #endregion

        #region ID  Properties
        [Category("Appearance"),
       DefaultValue("Simple of Text"),
       Description("ID"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual string myID
        {
            get
            {
                string text = (string)ViewState["myID"];
                return text ?? "Simple of Text";
            }
            set
            {
                ViewState["myID"] = value;
            }
        }
        #endregion

        #region Class  Properties
        [Category("Appearance"),
       DefaultValue("Simple of Text"),
       Description("Class"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual string myClass
        {
            get
            {
                string text = (string)ViewState["myClass"];
                return text ?? "Simple of Text";
            }
            set
            {
                ViewState["myClass"] = value;
            }
        }
        #endregion
        #region Name  Properties
        [Category("Appearance"),
       DefaultValue("Simple of Text"),
       Description("Name"),
       Localizable(true),
       PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual string myName
        {
            get
            {
                string text = (string)ViewState["myName"];
                return text ?? "Simple of Text";
            }
            set
            {
                ViewState["myName"] = value;
            }
        }
        #endregion

        #region Top Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("x of left point"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myTop
        {
            get
            {
                //int x = (int)ViewState["myX"];
                if (ViewState["myTop"] == null)
                    return 0;
                else
                    return (int)ViewState["myTop"];
            }
            set
            {
                ViewState["myTop"] = value;
            }
        }
        #endregion

        #region Left Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("x of left point"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myLeft
        {
            get
            {
                //int x = (int)ViewState["myX"];
                if (ViewState["myLeft"] == null)
                    return 0;
                else
                    return (int)ViewState["myLeft"];
            }
            set
            {
                ViewState["myLeft"] = value;
            }
        }
        #endregion
        #region Right Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("x of left point"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myRight
        {
            get
            {
                //int x = (int)ViewState["myX"];
                if (ViewState["myRight"] == null)
                    return 0;
                else
                    return (int)ViewState["myRight"];
            }
            set
            {
                ViewState["myRight"] = value;
            }
        }
        #endregion
        #region Bottom Properties
        [Category("Appearance"),
        DefaultValue("0"),
        Description("x of left point"),
        Localizable(true),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual int myBottom
        {
            get
            {
                //int x = (int)ViewState["myX"];
                if (ViewState["myBottom"] == null)
                    return 0;
                else
                    return (int)ViewState["myBottom"];
            }
            set
            {
                ViewState["myBottom"] = value;
            }
        }
        #endregion
        #endregion
        
        //protected override void CreateChildControls()
        //{
        //    ClientScriptManager cs = Page.ClientScript;
        //    Type rsType = this.GetType();

            
        //    Label lblText = new Label();
        //    //lblText.ID = "MyControl_Label1";
        //    lblText.Text = myText;
        //    lblText.Font.Name = myFontName;
        //    lblText.Font.Bold = myBold;
        //    lblText.Font.Italic = myItalic;
        //    lblText.Font.Strikeout = myStrikeThrough;
        //    lblText.Font.Underline = myUnderline;
        //    lblText.Font.Size = myFontSize;
        //    lblText.ForeColor = System.Drawing.Color.FromArgb(myTextColor);
        //    lblText.Width = myWidth;
        //    lblText.Height = myHeight;            
            
        //    switch(myAlign)
        //    {
        //        case 0:
        //            lblText.Style["Align"] = "Left";
        //            break;
        //        case 1:
        //            lblText.Style["Align"] = "Center";
        //            break;
        //        case 2:
        //            lblText.Style["Align"] = "Right";
        //            break;
        //        default:
        //            lblText.Style["Align"] = "Left";
        //            break;
        //    }

        //    lblText.Style["x"] = myX.ToString();
        //    lblText.Style["y"] = myY.ToString();
        //    lblText.Style["top"] = myTop.ToString();
        //    lblText.Style["left"] = myLeft.ToString();
        //    lblText.Style["right"] = myRight.ToString();
        //    lblText.Style["bottom"] = myBottom.ToString();

        //    this.Controls.Add(lblText);

        //    cs.RegisterClientScriptInclude("MyScript", cs.GetWebResourceUrl(rsType, "SimpleControl.MyScript.js"));

        //    base.CreateChildControls();
        //}

        public string GenerateHTML()
        {
            // Div
           
            Panel panel = new Panel();
            panel.ID = Guid.NewGuid().ToString();
           
          //  panel.Attributes.Add("runat","server");
            panel.Attributes.Add("style", "position: absolute; border-color: gray; border-style: dotted;top: 0px; left:0px");
         //   panel.Attributes.Add("style", "left: 0px");
          //  panel.Attributes.Add("style", "top: 0px");
            panel.Attributes.Add("style","width:"+ myTextLength);
            panel.CssClass = "drag";

            
            // Span
            Label lblText = new Label();
            lblText.ID = myID;
            lblText.Text = myText;
            lblText.Font.Name = myFontName;
            lblText.Font.Bold = myBold;
            lblText.Font.Italic = myItalic;
            lblText.Font.Strikeout = myStrikeThrough;
            lblText.Font.Underline = myUnderline;
            lblText.Font.Size = myFontSize;
            lblText.ForeColor = System.Drawing.Color.FromArgb(myTextColor);
            lblText.Width = myWidth;
            lblText.Height = myHeight;

            switch (myAlign)
            {
                case 0:
                    lblText.Style["Align"] = "Left";
                    break;
                case 1:
                    lblText.Style["Align"] = "Center";
                    break;
                case 2:
                    lblText.Style["Align"] = "Right";
                    break;
                default:
                    lblText.Style["Align"] = "Left";
                    break;
            }
            lblText.Style["top"] = myTop.ToString();
            lblText.Style["left"] = myLeft.ToString();
            lblText.Style["right"] = myRight.ToString();
            lblText.Style["bottom"] = myBottom.ToString();
            lblText.Style["display"]="inline";

            lblText.Attributes.Add("onclick", "setToHdfSelectedID(this)");

            panel.Controls.Add(lblText);
            //drag.Controls.Add(panel);
           return RenderControl(panel);
        }
    
        public string RenderControl(Control control)
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
                {
                    control.RenderControl(textWriter);
                }
            }
            return sb.ToString();
        }
    }
}
